package net.sourceforge.ehep.core;

import static org.junit.Assert.*;

import java.util.Vector;

import net.sourceforge.ehep.gui.HexTable;
import net.sourceforge.ehep.gui.HexTableItem;

import org.eclipse.swt.SWT;
import org.eclipse.ui.actions.SimpleWildcardTester;
import org.junit.Test;

public class bufferManagerTest {

	@Test
	public void testSetCurrFileBufSize() {
		bufferManager bufManager = new bufferManager();
		bufManager.setCurrFileBufSize(10);
		int size = bufManager.getCurrFileBufSize();
		assertEquals("10",Integer.toString(size));
	}
	
	@Test
	public void testGetArrayListRowIndex(){
		bufferManager bufManager = new bufferManager();
		HexTable htable = new HexTable();
		HexTableItem tableItem = new HexTableItem(htable,SWT.NONE);
		tableItem.getItem().setData("row1");
		htable.addItem(tableItem);
		assertTrue(bufManager.getArrayListRowIndex(1,htable)== 0);
	}
	
	@Test
	public void testGetArrayListColIndex(){
		bufferManager bufManager = new bufferManager();
		assertTrue(bufManager.getArrayListColIndex(18)== 3);
	}
	
	@Test
	public void testparseStringMethod(){
		bufferManager bufManager = new bufferManager();
		Vector<String> sign = new Vector<String>();
		bufManager.parseStringMethod(sign, "watermark.pad", ",");
		assertTrue(Integer.parseInt(sign.elementAt(0)) == 20);
		
	}

	@Test
	public void testModifyBufInsCharEnd(){
		bufferManager bufManager = new bufferManager();
		String str = "abcdefgh";
		byte[] b = str.getBytes();
		synchronized(FileManager.buffer){
			for(int i=0;i<b.length;i++){
				FileManager.buffer[i] = b[i];
			}
		}
		bufManager.setCurrFileBufSize(b.length);
		bufManager.modifyBufInsCharEnd("watermark.char");
		byte actByte = FileManager.buffer[bufManager.getCurrFileBufSize() - 1];
		String actStr = new String(new byte[] {actByte});
		//checks if the buffer has been modified with the watermark signature(character) 
		assertEquals("S", actStr);
	}

	@Test
	public void testModifyBufInsSignEnd(){
		bufferManager bufManager = new bufferManager();
		String str = "abcdefghijklmnopqr";
		byte[] b = str.getBytes();
		synchronized(FileManager.buffer){
			for(int i=0;i<b.length;i++){
				FileManager.buffer[i] = b[i];
			}
		}
		bufManager.setCurrFileBufSize(b.length);
		bufManager.modifyBufInsSignEnd("watermark.sign");
		byte[] actBytes = new byte[bufManager.getCurrFileBufSize()];
		for(int i=0;i<bufManager.getCurrFileBufSize();i++){
			actBytes[i] = FileManager.buffer[i];
		}
		String actStr = new String(actBytes);
		//checks if the buffer has been modified with the watermark signature 
		assertEquals("abcdefghiSIGNATURE", actStr);
	}

	@Test
	public void testVerifyBufInsCharEnd(){
		bufferManager bufManager = new bufferManager();
		String str = "abcS";
		byte[] b = str.getBytes();
		synchronized(FileManager.buffer){
			for(int i=0;i<b.length;i++){
				FileManager.buffer[i] = b[i];
			}
		}
		bufManager.setCurrFileBufSize(b.length);
		//verifies if the buffer has been watermarked		
		assertTrue(bufManager.verifyBufInsCharEnd("watermark.char"));
	}
	
	@Test
	public void testVerifyBufInsSignEnd(){
		bufferManager bufManager = new bufferManager();
		String str = "abcdefSIGNATURE";
		byte[] b = str.getBytes();
		synchronized(FileManager.buffer){
			for(int i=0;i<b.length;i++){
				FileManager.buffer[i] = b[i];
			}
		}
		bufManager.setCurrFileBufSize(b.length);
		//verifies if the buffer has been watermarked 		
		assertTrue(bufManager.verifyBufInsSignEnd("watermark.sign"));
	}
	
	@Test
	public void testRemoveBufCharEnd(){
		bufferManager bufManager = new bufferManager();
		String str = "abcdS";
		byte[] b = str.getBytes();
		synchronized(FileManager.buffer){
			for(int i=0;i<b.length;i++){
				FileManager.buffer[i] = b[i];
			}
		}
		bufManager.setCurrFileBufSize(b.length);
		bufManager.removeBufCharEnd("watermark.char");
		byte actByte = FileManager.buffer[bufManager.getCurrFileBufSize() - 1];
		String actStr = new String(new byte[] {actByte});
		//verifies if the character has been removed and replaced instead with "0"
		assertEquals("0", actStr);
	}
	
	@Test
	public void testRemoveBufSignEnd(){
		bufferManager bufManager = new bufferManager();
		String str = "abcdSIGNATURE";
		byte[] b = str.getBytes();
		synchronized(FileManager.buffer){
			for(int i=0;i<b.length;i++){
				FileManager.buffer[i] = b[i];
			}
		}
		bufManager.setCurrFileBufSize(b.length);
		bufManager.removeBufSignEnd("watermark.sign");
		byte[] actBytes = new byte[bufManager.getCurrFileBufSize()];
		for(int i=0;i<bufManager.getCurrFileBufSize();i++){
			actBytes[i] = FileManager.buffer[i];
		}
		String actStr = new String(actBytes);
		//verifies if the signature has been removed and replaced instead with 0's
		assertEquals("abcd000000000", actStr);
		
	}

}
